<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.food.model.Calorie" %>
<% 		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		//SQL Server
		String url = "jdbc:sqlserver://192.168.38.73:1433;DatabaseName=PJDB";
		//MySQL
		//String url = "jdbc:mysql://localhost:3306/Northwind";
		String query = "select calid,calfood,calunit,calcalorie from calorie where calfood=?";
		String name = request.getParameter("calfood");
		
		List< Calorie > result = null;
		Calorie Calorie;
		String output = "";
		String q="<h1>123</h1>";
		try{
			//MySQL
			//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			//conn = DriverManager.getConnection(url, "root", "123456");
			//SQL Server
			DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
			conn = DriverManager.getConnection(url, "sa", "sa123456");
			//stmt = conn.createStatement();
			//stmt.setString(1, name);
			stmt = conn.prepareStatement(query);
			stmt.setString(1,name);
			
			rs = stmt.executeQuery();
			
			result = new ArrayList<Calorie>();
			while(rs.next()){
				Calorie = new Calorie();
				Calorie.setCalid(rs.getInt("calid"));
				Calorie.setCalfood(rs.getString("calfood"));
				Calorie.setCalunit(rs.getString("calunit"));
				Calorie.setCalcalorie(rs.getDouble("calcalorie"));
				result.add(Calorie);
			}
			for(Calorie items:result){
				output+="<option name='calfood' value="+items.getCalid()+">"+items.getCalunit()+"</option>";
			}
					
			 out.print(output);
		}
		catch(SQLException e){
			out.println("Error:" + e.getMessage());
		}
		finally{
			if(rs != null){
			   rs.close();
			}
			if(stmt != null){
			 stmt.close();
			}
			if(conn != null){
			}
		}
		 
		//}
%>